library(haven)
library(readr)
library(dplyr)
library(stringr)

rm(list=ls())

# Load datasets, subset and generate objects
datasets <- dir("./raw_data/", pattern = "ESS[0-9]e[0-9][0-9]_[0-9].dta|ESS[0-9][0-9].dta", full.names = TRUE)
datasets <- datasets[c(2:10,1)]
years <- 2002+seq(0,18,2) # adapt
for(i in 1:10){
  data <- read_dta(datasets[i])
  data <- data %>% select(essround, idno, cntry, trstprl, trstlgl, 
                          trstplc, trstplt, trstep, trstun, agea, 
                          eduyrs, eisced, hincfel, lrscale, mnactic, gndr,
                          dweight, pweight, pspwght)
  
  # Education, income, employment, age
  
  data <- data %>% dplyr::rename(trust_parliament = trstprl, 
                                 trust_legalsystem = trstlgl, 
                                 trust_police = trstplc, 
                                 trust_politicians = trstplt, 
                                 trust_euparliament = trstep, 
                                 trust_un = trstun,
                                 age = agea,
                                 hincome_feeling = hincfel,
                                 leftright = lrscale,
                                 gender = gndr,
                                 mainact = mnactic)
  # names(data) <- paste(names(data), "_", str_extract(str_replace(i, "\\./", ""), "^.{4}"), sep="")
  if(i < 10){
  assign(paste(str_extract(str_replace(datasets[i], "\\./raw_data/", ""), "^.{4}"), "_", years[i], sep=""), data.frame(data))
  }else{
    assign(paste(str_extract(str_replace(datasets[i], "\\./raw_data/", ""), "^.{5}"), "_", years[i], sep=""), data.frame(data)) 
  }
  print(i)
}
rm(data, datasets, i, years)


# Create longformat
liste <- list(ESS1_2002, ESS2_2004, ESS3_2006, ESS4_2008, ESS5_2010, ESS6_2012, ESS7_2014, ESS8_2016, ESS9_2018, ESS10_2020)
# GENERATE LONG FORMAT DATASET
liste2 <- liste
years <- c("2002", "2004", "2006", "2008", "2010", "2012", "2014", "2016", "2018", "2020")# adapt
for (i in 1:10){
  liste2[[i]] <- liste2[[i]] %>% zap_labels () %>% mutate(year = years[i])
}
data <- bind_rows(liste2[[1]],liste2[[2]],liste2[[3]],liste2[[4]],
                  liste2[[5]],liste2[[6]],liste2[[7]],liste2[[8]],
                  liste2[[9]], liste2[[10]])

write_csv(data, "input_data.csv")